package com.bluegalaxy.nhb.snservice.common.utils;

import java.util.Random;

/**
 * Created by IntelliJ IDEA.
 *
 * @author:
 * @date: 2018/1/25
 * @time: 10:11
 * @description: 密码校验工具
 * To change this template use File | Settings | File Templates.
 */
public class PasswordUtil {

    public static final String NUM_STR = "23456789";
    public static final String NUM_ALL_STR = "0123456789";
    public static final char[] NUM = NUM_STR.toCharArray();
    public static final String LETTER = "ABCDEFGHJKMNPQRSTUVWXYZ";
    public static final String LETTER_ALL_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toUpperCase();
    public static final String LETTER_ALL_LOWER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toLowerCase();
    public static final char[] LETTER_UPPER = LETTER.toUpperCase().toCharArray();
    public static final char[] LETTER_LOWER = LETTER.toLowerCase().toCharArray();

    /**
     * 随机生成密码
     *
     * @param size
     * @return
     */
    public static String newRandomPassword(int size) {
        int index;
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < size; i++) {
            switch (i % 3) {
                case 0:
                    index = r.nextInt(NUM.length);
                    sb.append(NUM[index]);
                    break;
                case 1:
                    index = r.nextInt(LETTER_UPPER.length);
                    sb.append(LETTER_UPPER[index]);
                    break;
                case 2:
                    index = r.nextInt(LETTER_LOWER.length);
                    sb.append(LETTER_LOWER[index]);
                    break;
            }
        }
        return sb.toString();
    }

    /**
     * 校验密码是否合格
     *
     * @param password
     * @return
     */
    public static boolean checkPassword(String password) {
        if (password == null || password.length() < 6) {
            return false;
        }
        String[] passwords = password.split("");
        boolean tagNum = false, tagLetterUpper = false, tagLetterLower = false;
        for (int i = 0; i < passwords.length; i++) {
            String s = passwords[i];
            if (NUM_ALL_STR.contains(s)) {
                tagNum = true;
            }
            if (LETTER_ALL_UPPER.contains(s)) {
                tagLetterUpper = true;
            }
            if (LETTER_ALL_LOWER.contains(s)) {
                tagLetterLower = true;
            }
        }
        return tagNum && tagLetterUpper && tagLetterLower;
    }

}
